package com.jcgyl.export.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jcgyl.export.entity.Customer;
import com.jcgyl.export.entity.CustomerQueryModel;
import com.jcgyl.export.service.CustomerService;
import com.jcgyl.export.util.entity.Result;

@RestController
@RequestMapping("/customer")
public class CustomerController {
	@Autowired
	private CustomerService customerService;

	// 获取所有的客户信息
	@PostMapping("/findAllCustomer")
	public Map<String, Object> doFindAllCustomer(@RequestBody CustomerQueryModel customerFilter) {
		String name = customerFilter.getFilter().getName();
		String code = customerFilter.getFilter().getCode();
		String contact = customerFilter.getFilter().getContact();
		String phone = customerFilter.getFilter().getPhone();
		String email = customerFilter.getFilter().getEmail();
		System.out.println("接受客户前台客户信息" + ":" + name + ":" + code + ":" + contact + ":" + phone + ":" + email);
		int pageIndex = customerFilter.getPageIndex();
		int pageSize = customerFilter.getPageSize();
		System.out.println("pageIndex:" + pageIndex + "pageSize:" + pageSize);
		Map<String, Object> map = customerService.findAllCustomer(name, code, contact, phone, email, pageIndex,
				pageSize);
		return map;
	}

	// 添加或修改客户信息
	@RequestMapping("/addOrUpdateCustomer")
	public Result doAndCustomer(@RequestBody Customer customer) {
		String name = customer.getName();
		String code = customer.getCode();
		System.out.println("添加客户的姓名" + ":" + name + ":" + code);
		if (customer.getCustomerId() == null) {
			return customerService.addCustomer(customer);
		} else {
			return customerService.updateCustomer(customer);
		}
	}

	// 禁用启用客户
	@RequestMapping("/changeStateCustomer")
	public Result doChangeStateCustomer(@RequestBody Customer customer) {
		int isDelete = customer.getIsDelete();
		int customerId = customer.getCustomerId();
		System.out.println("禁用启用　　customerId:" + customerId + "isDelete:" + isDelete);
		return customerService.changeState(customerId, isDelete);

	}

	// 删除客户信息
	@RequestMapping("/deleteCustomer")
	public Result Delete(@RequestBody Customer customer) {
		int customerId = customer.getCustomerId();
		if (customerId <= 0)
			return Result.build(1, "请重新选择");
		System.out.println("删除客户的Id" + customerId);
		return customerService.deleteCustomer(customerId);

	}
}
